<?php

namespace App\Http\Controllers\OperationManagement;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\OperationManagement\SanitationCategory;
use App\Models\OperationManagement\SanitationCategoryFile;
use Illuminate\Database\QueryException;
use App\Http\Requests\OperationManagement\SanitationCategoryRequest;
use Illuminate\Support\Facades\DB;

/**
 * @group 基础信息管理->环卫->站点管理->站点类型
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class SanitationCategoryController extends Controller
{
    /**
     * 站点类型列表---分页
     *
     * @queryParam name string 站点类型名称
     * @queryParam files->file->local_url string 附件url地址
     */
    public function index(Request $request)
    {
        $per_page = $request->input('per_page',10);
        $data = SanitationCategory::orderBy('id','desc')->with('files')->paginate($per_page);
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 站点类型列表---不分页
     *
     * @queryParam name string 站点类型名称
     * @queryParam files->file->local_url string 附件url地址
     */
    public function getAll(Request $request)
    {
        $data = SanitationCategory::orderBy('id','desc')->with('files')->get();
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增站点类型
     *
     * @bodyParam name string required 站点类型名称
     * @bodyParam category_ids string 站点设备类型ids:1,2,3
     *
     * @queryParam name string 站点类型名称
     * @queryParam category_ids string 站点设备类型ids:1,2,3
     */
    public function store(SanitationCategoryRequest $request)
    {
        DB::beginTransaction();
        try {
            $data = SanitationCategory::create($request->all());
            if($request->has('category_ids')){
                $file_data = [];
                $fileArr = explode(',',$request->get('category_ids'));
                foreach ($fileArr as $k=>$value){
                    $tmp = [];
                    $tmp['file_id'] = $value;
                    $tmp['category_id'] = $data->id;
                    array_push($file_data,$tmp);
                }
                try{
                    SanitationCategoryFile::insert($file_data);
                }catch (\Exception $e){
                    DB::rollBack();
                    return response()->json(encodeMsg(["message" => "请先确认图片是否已经添加或者图片id是否重复".$e->getMessage()],404), 404);
                }
            }
            DB::commit();
            return response()->json(encodeMsg($data,201));
        } catch (QueryException $e) {
            DB::rollBack();
            return response()->json(encodeMsg(['message' => '站点类型添加失败!'],400));
        }
    }

    /**
     * 查看站点类型
     *
     * @bodyParam id int required 站点类型ID
     *
     * @queryParam name string 站点类型名称
     * @queryParam files->file->local_url string 附件url地址
     *
     */
    public function show($id)
    {
        // 创建模型
        $data = SanitationCategory::with('files')->find($id);
        if($data == []) {
            return response()->json(encodeMsg(["message" => "没有该站点类型!"], 404));
        }
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 修改站点类型
     *
     * @bodyParam  id  int 站点类型ID
     * @bodyParam  name string required 站点类型名称
     * @bodyParam category_ids string 站点设备类型ids:1,2,3
     *
     * @queryParam name string 站点类型名称
     * @queryParam category_ids string 站点设备类型ids:1,2,3
     */
    public function update(SanitationCategoryRequest $request,$id)
    {
        // 创建模型
        $sanitationCategory = SanitationCategory::with('files')->find($id);
        if($sanitationCategory == []) {
            return response()->json(encodeMsg(["message" => "没有该站点类型!"], 404));
        }
        DB::beginTransaction();
        try {
            if($request->has('category_ids')){
                $origin_files = SanitationCategoryFile::where('category_id',$id)->get()->toArray();//图片原来的id号是好多 取出
                $origin_file_ids = array();//这个数组用于报错以前的所有file的id号
                if($origin_files != []){
                    foreach ($origin_files as $v){
                        array_push($origin_file_ids,$v['id']);
                    }
                }
                $file_data = [];
                $fileArr = explode(',',$request->get('category_ids'));
                foreach ($fileArr as $k=>$value){
                    $tmp = [];
                    $tmp['file_id'] = $value;
                    $tmp['category_id'] = $id;
                    array_push($file_data,$tmp);
                }
                try{
                    SanitationCategoryFile::whereIn('id',$origin_file_ids)->delete();
                    SanitationCategoryFile::insert($file_data);
                }catch (\Exception $e){
                    DB::rollBack();
                    return response()->json(encodeMsg(["message" => "请先确认图片是否已经添加或者图片id是否重复".$e->getMessage()],404), 404);
                }
            }
            $sanitationCategory->update($request->all());
            DB::commit();
            return response()->json(encodeMsg($sanitationCategory,200));
        } catch (QueryException $e) {
            DB::rollBack();
            return response()->json(encodeMsg(['message' => '站点类型修改失败!'],400));
        }
    }

    /**
     * 删除站点类型
     *
     * @bodyParam id int required required 站点类型ID
     * @response 204 {
     *   "message": "删除成功"
     * }
     *
     *
     * @response 404 {
     *  "message": "没有该站点类型"
     * }
     */
    public function destroy($id)
    {
        // 创建模型
        $sanitationCategory = SanitationCategory::find($id);
        if($sanitationCategory == []) {
            return response()->json(encodeMsg(["message" => "没有该站点类型!"], 404));
        }
        if($sanitationCategory->delete()) {
            return response()->json(encodeMsg(["message" => "删除成功!"], 204));
        } else {
            return response()->json(encodeMsg(["message" => "删除失败!"], 400));
        }
    }
}
